package freshmanspecial.mredrock.com.welcomefreshman.Model;


import android.util.Log;

import freshmanspecial.mredrock.com.welcomefreshman.Bean.FailRatioBean;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.SexRatioBean;
import freshmanspecial.mredrock.com.welcomefreshman.Bean.WorkRatioBean;
import freshmanspecial.mredrock.com.welcomefreshman.Service.DataService;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/8/8 0008.
 */

public class DataModel implements IDataModel {
    private String api = "http://www.yangruixin.com/";

    public interface OnGetData{
        void onFinish(Object dataBean);
    }
    @Override
    public void getSexRatio(final OnGetData onGetData) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(api)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        DataService service = retrofit.create(DataService.class);
        service.getSexRatio("SexRatio").
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SexRatioBean>() {
                    SexRatioBean bean = null;
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SexRatioBean value) {
                        Log.d("12333",value.getData().get(0).getCollege());
                        bean = value;
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if(bean != null) onGetData.onFinish(bean);
                    }
                });
    }

    @Override
    public void getFailRatio(final OnGetData onGetData) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(api)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        DataService service = retrofit.create(DataService.class);
        service.getFailRatio("FailRatio").
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FailRatioBean>() {
                    FailRatioBean bean = null;
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FailRatioBean value) {
                        bean = value;
                    }
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if(bean != null) onGetData.onFinish(bean);
                    }
                });
    }

    @Override
    public void getWorkRatio(final OnGetData onGetData) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(api)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        DataService service = retrofit.create(DataService.class);
        service.getWorkRatio("WorkRatio").
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WorkRatioBean>() {
                    WorkRatioBean bean = null;
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WorkRatioBean value) {
                        bean = value;
                    }
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if(bean != null) onGetData.onFinish(bean);
                    }
                });
    }
}
